<?php

namespace Home\Controller;

use Think\Controller;

class LoginController extends Controller
{
    public function auth()
    {
        $username = I('post.username');
        $password = xmd5(I('post.password'));
        $Admin = M('Admin');
        $data = $Admin->where("username='%s' and password='%s'", [$username, $password])->find();
        if ($data) {
            session('Admin', $data);
            $this->ajaxReturn(array(
                'code' => 1,
                'info' => '登陆成功',
                'session' => session('Admin')
            ));
        } else {
            $this->ajaxReturn(array(
                'code' => 0,
                'info' => '用户名或密码错误'
            ));
        }
    }

    public function check()
    {
        if (session('Admin')) {
            $this->ajaxReturn([
                'code' => 1,
                'info' => '登录检查成功',
                'data' =>session('Admin')
            ]);
        } else {
            $this->ajaxReturn([
                'code' => -1,
                'info' => '登录失效，请重新登录！'
            ]);
        }
    }

    public function logout()
    {
        session('Admin', null);
        $this->ajaxReturn([
            'code' => 1,
            'info' => '退出成功'
        ]);
    }
}